<?php
namespace app\admin\controller;
use app\admin\controller\Permissions;
use think\Config;
use \think\Db;
use \think\Cookie;
use \think\Session;
use \think\Cache;
class Hqjiesuan extends Permissions
{
    //回迁结算列表
    public function hqjiesuan()
    {
        $list = db('population')->where(['isdel'=>1])->select();
        foreach ($list as $k => &$v) {
            $v['jinesum']=db('hqjiesuan')->where(['uid'=>$v['id']])->value('jinesum');
        }
        $this->assign('list',$list);
        return $this->fetch();
    }

    public function add(){
        $huzhu = db('population')->where(['isdel'=>1])->select();
        if (request()->isAjax()) {
            $input = input('post.');
            // dump($input);die;
            $isok = db('summarys')->insert($input);
            $arr = [];
            if ($isok) {
                $arr['code'] = 1;
            } else {
                $arr['code'] = 2;
                $arr['msg'] = '操作失败,请刷新重试';
            }
            return $arr;
        }
        $this->assign('huzhu',$huzhu);
        return $this->fetch();
    }

    public function edit(){
        $input = input('get.');

        if (request()->isAjax()) {
            $input = input('post.');
            $id = $input['id'];
            $isok = db('summarys')->where(['id' =>$id])->update($input);
            $arr = [];
            if ($isok) {
                $arr['code'] = 1;
            } else {
                $arr['code'] = 2;
                $arr['msg'] = '操作失败,请刷新重试';
            }
            return $arr;
        }
        $data = db('summarys')->where(['id' => $input['id']])->find();
        $this->assign('data',$data);

        $huzhu = db('population')->where(['isdel'=>1])->select();
        $this->assign('huzhu',$huzhu);

        return $this->fetch();
    }

    public function delete()
    {
        if (request()->isAjax()) {
            $id = input('id');
            $isok = db('summarys')->where('id', $id)->delete();
            $arr = [];
            if ($isok) {
                $arr['code'] = 1;
                $arr['msg'] = "删除成功！";
            } else {
                $arr['code'] = 2;
                $arr['msg'] = "删除失败！";
            }
            return $arr;
        }
    }
    // 回迁选房汇总
      public function summarys()
    {

        $list = db('summarys')->select();

        //$list = db('population')->where(['isdel'=>1])->select(); chainum pingnum name
        foreach ($list as $k => $v) {
            $userInfo = db('population')->where(['id'=>$v['pid']])->find();
            $list[$k]['chainum'] = $userInfo['chainum'];
            $list[$k]['pingnum'] = $userInfo['pingnum'];
            $list[$k]['name'] = $userInfo['name'];
            //$v['jinesum']=db('hqjiesuan')->where(['uid'=>$v['id']])->value('jinesum');
        }
        $this->assign('list',$list);
        $admin_cate_id = Session::get('admin_cate_id');
        $this->assign('admin_cate_id',$admin_cate_id);
        $operation = Config::get('operation');
        $this->assign('operation',$operation);
        return $this->fetch();
    }
    //回迁结算录入
    public function hqjiesuan_add()
    {
        $id = input('id');
        $this->assign('id',$id);
        $zhu = db('population')->where(['id'=>$id])->find();
        $zhu['chairen'] = db('popinfo')->where(['isdel'=>1,'pid'=>$id])->count('id');
        $zhu['chaicun'] = db('popinfo')->where(['isdel'=>1,'pid'=>$id,'iscun'=>'是'])->count('id');
        $zhu['chaifei'] = db('popinfo')->where(['isdel'=>1,'pid'=>$id,'iscun'=>'否'])->count('id');

        $info = db('hqjiesuan')->where(['uid'=>$id])->find();
        $this->assign('info',$info);
        if (request()->isAjax()) {
            $input = input('post.');
            // dump($input);die;
            $input['create_time'] = date('Y-m-d',time());
            //pp($input);exit;
            $arr = [];
            $onlineInfo = db('hqjiesuan')->where(['uid'=>$input['uid']])->find();
            if($onlineInfo['id']){
               $isok = db('hqjiesuan')->where('id',$onlineInfo['id'])->update($input);
            }else{
			  $isok = db('hqjiesuan')->insert($input);
			}
			if ($isok) {
				$arr['code'] = 1;
			} else {
				$arr['code'] = 2;
				$arr['msg'] = '操作失败,请刷新重试';
			}
			return $arr;
        }
        $this->assign('zhu',$zhu);
        $admin_cate_id = Session::get('admin_cate_id');
        $this->assign('admin_cate_id',$admin_cate_id);
        $operation = Config::get('operation');
        $this->assign('operation',$operation);
        return $this->fetch(); 
    }

    

}
